//
//  PainNextViewController.m
//  MaJangJi
//
//  Created by 嗯，大葱 on 16/9/9.
//  Copyright © 2016年 嗯，大葱. All rights reserved.
//

#import "PainNextViewController.h"
#import "FangAn1TableViewCell.h"
#import "FN4TableViewCell.h"
#import "FN5TableViewCell.h"
#import "Second1TableViewCell.h"
#import "Second2TableViewCell.h"
#import "Second3TableViewCell.h"
#import "SubmitOrderViewController.h"
#import "ListDiyDEditTableViewCell.h"
#import "AddCustomViewController.h"
#define k_Width      [UIScreen mainScreen].bounds.size.width

#define h_Height     [UIScreen mainScreen].bounds.size.height

@interface PainNextViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    int  x;
    int x1;
    int x2;
    int x3;
    
}
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) UITextField *field;

@end

@implementation PainNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    x = 0;
    x1 = 0;
    x2 = 0;
    x3 = 0;
    [self setNavBar];
    [self configureViewController];
}
- (void)setNavBar {
    
    self.navigationItem.title = @"案例预定";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStylePlain) target:self action:@selector(handleBack2)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"问"] style:(UIBarButtonItemStylePlain) target:self action:@selector(handleWenAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor grayColor];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];

    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor whiteColor];
//    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, -20, k_Width, 20)];
//    statusBarView.backgroundColor=[UIColor colorWithRed:82/255.0 green:82/255.0 blue:82/255.0 alpha:1.0];
//    [self.navigationController.navigationBar addSubview:statusBarView];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
#pragma mark 导航栏按钮点击事件
- (void)handleBack2 {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)handleWenAction {
   
    
   
    
    
}

#pragma mark 配置控制器视图
- (void)configureViewController {
   
    
     self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, k_Width, h_Height - 40) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];

    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
    UIButton *sureBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    sureBtn.frame = CGRectMake(0, h_Height - 40, k_Width, 40);
    [sureBtn setTitle:@"确定" forState:(UIControlStateNormal)];
    sureBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:67/255.0 blue:86/255.0 alpha:1.0];
    [sureBtn addTarget:self action:@selector(handleTurnViewController) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:sureBtn];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FangAn1TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
   
    [self.tableView registerNib:[UINib nibWithNibName:@"FN4TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FN5TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell5"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"Second1TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell7"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"Second2TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell8"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"Second3TableViewCell"
        bundle:nil]forCellReuseIdentifier:@"cell9"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ListDiyDEditTableViewCell" bundle:nil] forCellReuseIdentifier:@"diycell"];
    
    
    self.tableView.allowsSelection = NO;
    //隐藏多余cell
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark tableView代理事件
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    if (section == 1) {
        return 5;
    }
    if (section == 2) {
        return 1;
    }
    if (section == 3) {
        return 1;
    }
    if (section == 4) {
        return 1;
    }
    else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            Second1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell7"];
            self.field = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-120, 10, 70, 20)];
            self.field.placeholder = @"输入面积";
            [self.field setFont:[UIFont systemFontOfSize:16]];
            [cell.contentView addSubview:self.field];

//            [cell.fixBtn addTarget:self action:@selector(handelFixAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
            return cell;
        }
        else {
            FangAn1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
            cell.nameLabel.text = @"施工门店";
            cell.moneyLabel.text = @"珺墨网络科技";
            return cell;
        }
        
        
    }
    
    else if (indexPath.section == 1){
            ListDiyDEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"diycell"];
        cell.number = 45;
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"0"]) {
            cell.addButton.alpha = 0;
            cell.deleButton.alpha = 0;
            cell.remoButton.alpha = 0;
            cell.numLabel.font = [UIFont systemFontOfSize:18];
            
        }
        else{
            
            cell.addButton.alpha = 1;
            cell.deleButton.alpha = 1;
            cell.remoButton.alpha = 1;
            cell.numLabel.font = [UIFont systemFontOfSize:14];
            
            
        }

//            return cell;
//        }else {
//            Second2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell8"];
//            [cell.downBtn addTarget:self action:@selector(handleDownBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
//            [cell.upBtn addTarget:self action:@selector(handleUpBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//            if (indexPath.row == 1) {
//                cell.nameLael.text = @"麻将桌";
////                cell.moenyLabel.text = [NSString stringWithFormat:@"%d",x1];
//            }
//            if (indexPath.row == 2) {
//                cell.nameLael.text = @"键盘";
////                cell.moenyLabel.text = [NSString stringWithFormat:@"%d",x2];
//            }
//            if (indexPath.row == 3) {
//                cell.nameLael.text = @"椅子";
////                cell.moenyLabel.text = [NSString stringWithFormat:@"%d",x3];
//            }
//            if (indexPath.row == 4) {
//                cell.nameLael.text = @"水晶牌";
////                cell.moenyLabel.text = [NSString stringWithFormat:@"%d",x4];
//            }
        
          
            
            return cell;
//        }
    }
    else if (indexPath.section == 3)
    {
        FN4TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell4"];
        return cell;
    }
    
    else if (indexPath.section == 4)
    {
        Second3TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell9"];
        return cell;

    }
    else {
        FangAn1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if(indexPath.section == 2){
        
                cell.nameLabel.text = @"设计费";
                cell.moneyLabel.text = @"¥666";
            
        }
        
        return cell;
    }
   
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 88, 25)];
        titleLabel.text = @"定制物料";
        [titleView addSubview:titleLabel];
        
        UIButton *ediLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        ediLabel.frame = CGRectMake(SCREEN_WIDTH - 160, 5, 18, 18);
        [ediLabel setImage:[UIImage imageNamed:@"but_modify"] forState:UIControlStateNormal];
        [ediLabel addTarget:self action:@selector(ediAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:ediLabel];
        
        UIButton *ediButton = [UIButton buttonWithType:UIButtonTypeCustom];
        ediButton.frame = CGRectMake(SCREEN_WIDTH - 140, 5, 44, 22);
        
        [ediButton setTitle:@"编辑" forState:UIControlStateNormal];
        [ediButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"1"]) {
            [ediButton setTitle:@"保存" forState:UIControlStateNormal];
            
        }
        [ediButton addTarget:self action:@selector(ediAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:ediButton];
        
        UIButton *addLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        addLabel.frame = CGRectMake(SCREEN_WIDTH - 90, 5, 18, 18);
        [addLabel setImage:[UIImage imageNamed:@"btn_add_wuliao@2x"] forState:UIControlStateNormal];
        
        [addLabel addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:addLabel];
        
        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addButton.frame = CGRectMake(SCREEN_WIDTH - 70, 5, 44, 22);
        [addButton setTitle:@"添加" forState:UIControlStateNormal];
        [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [addButton addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:addButton];
        
        
        
        return titleView;
    }
    return nil;
}
-(void)ediAction:(UIButton *)bt{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"0"]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"ListDiyEdit"];
        [self.tableView reloadData];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"ListDiyEdit"];
        
        //        [[NSNotificationCenter defaultCenter] postNotificationName:@"numAdd" object:nil];
        [self.tableView reloadData];
    }
    
}
-(void)addAction:(UIButton *)b{
    AddCustomViewController *cus = [[AddCustomViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:cus];
    
    [self presentViewController:naVC animated:YES completion:nil];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 40;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 3) {
        return 80;
    }
    else if(indexPath.section == 4){
        return 100;
    }
    else {
        return 45;
    }
}
//跳转到下一个控制器
- (void)handleTurnViewController {
    SubmitOrderViewController *sub = [[SubmitOrderViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:sub];
    [self presentViewController:naVC animated:YES completion:nil];
}

//修改面积按钮点击事件
//- (void)handelFixAction:(UIButton *)sender {
//     Second1TableViewCell  * cell = (Second1TableViewCell *)[[sender superview] superview];
//    cell.fixMianJTF.userInteractionEnabled = YES;
//    
//}

#pragma mark cell里面加减号按钮的点击事件
//- (void )handleDownBtnAction :(UIButton *)sender {
//    
//    Second2TableViewCell  * cell = (Second2TableViewCell *)[[sender superview] superview];
//    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
//    if (indexPath.row == 1) {
//        if (x>0) {
//            x--;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x*10];
//        }else {
//            [self showLog:@"if you click me again， I will be 0 down"];
//        }
//    }
//    if (indexPath.row == 2) {
//        if (x1>0) {
//            x1--;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x1];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x1*8];
//        }else {
//            [self showLog:@"if you click me again， I will be 0 down"];
//        }
//    }
//    if (indexPath.row == 3) {
//        if (x2>0) {
//            x2--;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x2];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x2*11];
//        }else {
//            [self showLog:@"if you click me again， I will be 0 down"];
//        }
//    }
//    if (indexPath.row == 4) {
//        if (x3>0) {
//            x3--;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x3];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x3*123];
//        }else {
//            [self showLog:@"if you click me again， I will be 0 down"];
//        }
//    }
//   
//}


//- (void )handleUpBtnAction:(UIButton *)sender {
//    Second2TableViewCell  * cell = (Second2TableViewCell *)[[sender superview] superview];
//    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
//    if (indexPath.row == 1) {
//        x++;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x*10];
//       
//    }
//    if (indexPath.row == 2) {
//        x1++;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x1];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x1*8];
//      
//    }
//    if (indexPath.row == 3) {
//        x2++;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x2];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x2*11];
//     
//    }
//    if (indexPath.row == 4) {
//        x3++;
//            cell.mountLabel.text = [NSString stringWithFormat:@"%d",x3];
//            cell.moenyLabel.text = [NSString stringWithFormat:@"¥%d",x3*123];
//    }
//
//}


- (void)showLog:(NSString *)message {
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"please see the message" message:message delegate:self cancelButtonTitle:@"cencle" otherButtonTitles:@"sure", nil];
    
    [alter show];
}
@end
